package com.ruoyi.project.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.blog.domain.BComment;
import com.ruoyi.project.blog.domain.BUser;
import com.ruoyi.project.blog.domain.vo.ApiCommentListVO;
import com.ruoyi.project.blog.domain.vo.SystemCommentVO;
import com.ruoyi.project.blog.mapper.BCommentMapper;
import com.ruoyi.project.blog.mapper.BUserMapper;
import com.ruoyi.project.blog.service.IBCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评论Service业务层处理
 *
 * @date 2023-10-16
 */
@Service
public class BCommentServiceImpl extends ServiceImpl<BCommentMapper, BComment> implements IBCommentService {
    @Autowired
    private BCommentMapper bCommentMapper;
    @Autowired
    private BUserMapper bUserMapper;

    /**
     * 查询评论
     *
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public BComment selectBCommentById(Long id) {
        return bCommentMapper.selectBCommentById(id);
    }

    /**
     * 查询评论列表
     *
     * @param bComment 评论
     * @return 评论
     */
    @Override
    public List<BComment> selectBCommentList(BComment bComment) {
        return bCommentMapper.selectBCommentList(bComment);
    }

    /**
     * 新增评论
     *
     * @param bComment 评论
     * @return 结果
     */
    @Override
    public int insertBComment(BComment bComment) {
        bComment.setCreateTime(DateUtils.getNowDate());
        return bCommentMapper.insertBComment(bComment);
    }

    /**
     * 修改评论
     *
     * @param bComment 评论
     * @return 结果
     */
    @Override
    public int updateBComment(BComment bComment) {
        return bCommentMapper.updateBComment(bComment);
    }

    /**
     * 批量删除评论
     *
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteBCommentByIds(Long[] ids) {
        return bCommentMapper.deleteBCommentByIds(ids);
    }

    /**
     * 删除评论信息
     *
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteBCommentById(Long id) {
        return bCommentMapper.deleteBCommentById(id);
    }

    @Override
    public List<SystemCommentVO> listComment(String keywords) {
        List<SystemCommentVO> list = bCommentMapper.selectPageList(keywords);
        return list;
    }

    @Override
    public List<ApiCommentListVO> selectCommentByArticleId(Long articleId) {
        //获取评论父级评论
        List<ApiCommentListVO> list = bCommentMapper.selectCommentPage(articleId);
        //获取子级
        for (ApiCommentListVO vo : list) {
            List<BComment> commentList = bCommentMapper.selectList(
                    new LambdaQueryWrapper<BComment>().eq(BComment::getParentId, vo.getId()).orderByDesc(BComment::getCreateTime));
            for (BComment e : commentList) {
                BUser replyUserInfo = bUserMapper.getByUserId(e.getReplyUserId());
                BUser user = bUserMapper.getByUserId(e.getUserId());
                ApiCommentListVO apiCommentListVO = ApiCommentListVO.builder()
                        .id(e.getId())
                        .userId(e.getUserId())
                        .replyUserId(e.getReplyUserId())
                        .nickname(user.getNickName())
                        .webSite("")
                        .replyNickname(replyUserInfo.getNickName())
                        .replyWebSite("")
                        .content(e.getContent())
                        .avatar(user.getAvatar())
                        .createTime(e.getCreateTime())
                        .browser(e.getBrowser())
                        .browserVersion(e.getBrowserVersion())
                        .system(e.getSystem())
                        .systemVersion(e.getSystemVersion())
                        .ipAddress(e.getIpAddress())
                        .build();
                vo.getChildren().add(apiCommentListVO);
            }
        }
        return list;
    }
}
